package cc.lucien.demo02.homework;

import java.util.Arrays;

public class SelectionSort {

    private SelectionSort(){}

    public static <E extends Comparable<E>> void sort(E[] arr){
        for (int i = arr.length - 1; i >= 0; i--) {
            int maxIndex = i;
            for (int j = i; j >= 0; j--) {
                if (arr[j].compareTo(arr[i]) > 0)
                    maxIndex = j;
            }
            if (maxIndex != i)
                swap(arr, i ,maxIndex);
        }
    }

    private static <E> void swap(E[] arr, int i, int maxIndex) {
        E temp = arr[i];
        arr[i] = arr[maxIndex];
        arr[maxIndex] = temp;
    }

    public static void main(String[] args) {
        Integer[] arr = {8,9,4,3,78,65,21};
        SelectionSort.sort(arr);
        System.out.println(Arrays.toString(arr));
    }
}
